package com.nas.crocus.controller;

import java.util.ArrayList;
import java.util.Collection;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nas.crocus.domain.Evento;
import com.nas.crocus.domain.Vuelo;
import com.nas.crocus.dto.ItemDto;
import com.nas.crocus.service.AlojamientoService;
import com.nas.crocus.service.EventoService;
import com.nas.crocus.service.HotelService;
import com.nas.crocus.service.VueloService;

@Controller
@RequestMapping(value = "/page/evento")
public class EventoPageController {

    @Autowired
    private EventoService eventoService;
    
    @Autowired
    private HotelService hotelService;
    
    @Autowired
    private AlojamientoService alojamientoService;
    
    @Autowired
    private VueloService vueloService;
    
    @Resource(name="menu")
    private Collection<ItemDto> menu;
    
    @RequestMapping(method = RequestMethod.GET)
    public String eventos(Model model) {
        
        model.addAttribute("menu", menu);
        model.addAttribute("breadcrumb", getBreadcrumb());
        model.addAttribute("eventos", eventoService.buscar());
        return "eventoListado";
    }
    
    @RequestMapping(value = "/edicion/id/{id}", method = RequestMethod.GET)
    public String edicion(@PathVariable Long id, Model model) {
        Evento evento = eventoService.buscar(id);
        if (evento == null) {
            model.addAttribute("evento", "Error no se encontro evento con identificador [" + id + "] para editar");
            return "error";
        }
        
        if (evento.getRuta() != null) {
            evento.getRuta().setVuelos(vueloService.buscarPorRutaId(evento.getRuta().getId()));
            for (Vuelo vuelo : evento.getRuta().getVuelos()) {
                vuelo.setRuta(null);
            }
        }
        ItemDto item = new ItemDto();
        item.setDescripcion(evento.getNombre());
        item.setUrl("/app/page/evento/edicion/id/"+id);
        model.addAttribute("menu", menu);
        Collection <ItemDto> breadcrumb = getBreadcrumb();
        breadcrumb.add(item);
        
        model.addAttribute("breadcrumb", breadcrumb);
        
        model.addAttribute("evento", evento);
        model.addAttribute("alojamientos", alojamientoService.buscarPorEventoId(id));
        model.addAttribute("hoteles", hotelService.buscar());
        
        
        return "eventoFormulario";
    }
    
    @RequestMapping(value = "/alta", method = RequestMethod.GET)
    public String alta(Model model) {
        ItemDto item = new ItemDto();
        item.setDescripcion("Nuevo evento");
        item.setUrl("/app/page/evento/alta");
        model.addAttribute("menu", menu);
        Collection <ItemDto> breadcrumb = getBreadcrumb();
        breadcrumb.add(item);
        model.addAttribute("breadcrumb", breadcrumb);
        
        model.addAttribute("hoteles", hotelService.buscar());
        return "eventoFormulario";
    }
    
    private Collection<ItemDto> getBreadcrumb(){
        ArrayList<ItemDto> breadcum = new ArrayList<ItemDto>();
        ItemDto item = new ItemDto();
        item.setDescripcion("Eventos");
        item.setUrl("/app/page/evento");
        breadcum.add(item);
        return breadcum;
    }
    
}
